package org.nyt.medicalstore.service.impl;

import org.nyt.medicalstore.model.Customer;
import org.nyt.medicalstore.repository.CustomerRepository;
import org.nyt.medicalstore.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerRepository customerRepository;

    // 基本的 CRUD 操作实现
    @Override
    public Customer saveCustomer(Customer customer) {
        return customerRepository.save(customer);
    }

    @Override
    public Customer getCustomerById(Long id) {
        return customerRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Customer not found with id: " + id));
    }

    @Override
    public List<Customer> getAllCustomers() {
        return customerRepository.findAll();
    }

    @Override
    public void deleteCustomer(Long id) {
        customerRepository.deleteById(id);
    }

    // 智能客服功能实现
    @Override
    public Map<String, Object> handleCustomerQuery(String query, Long customerId) {
        // 验证客户是否存在
        if (!customerRepository.existsById(customerId)) {
            throw new RuntimeException("Customer not found with id: " + customerId);
        }
        
        Map<String, Object> response = new HashMap<>();
        
        // 简单的关键词匹配
        if (query.toLowerCase().contains("order") || query.toLowerCase().contains("purchase")) {
            response.put("type", "order_query");
            response.put("message", "I can help you with your order. Would you like to see your order history?");
        } else if (query.toLowerCase().contains("delivery") || query.toLowerCase().contains("shipping")) {
            response.put("type", "delivery_query");
            response.put("message", "I can help you track your delivery. Please provide your order number.");
        } else {
            response.put("type", "general_query");
            response.put("message", "How can I assist you today?");
        }
        
        return response;
    }

    @Override
    public List<Map<String, Object>> getCustomerOrderHistory(Long customerId) {
        // 验证客户是否存在
        if (!customerRepository.existsById(customerId)) {
            throw new RuntimeException("Customer not found with id: " + customerId);
        }
        
        // 这里应该调用订单服务获取订单历史
        // 目前返回空列表作为示例
        return new ArrayList<>();
    }

    @Override
    public Map<String, Object> handleOrderIssue(String issue, Long orderId) {
        Map<String, Object> response = new HashMap<>();
        response.put("orderId", orderId);
        response.put("status", "received");
        response.put("message", "Your issue has been received and will be processed shortly.");
        return response;
    }

    @Override
    public Map<String, Object> getCustomerInfo(Long customerId) {
        Customer customer = getCustomerById(customerId);
        Map<String, Object> info = new HashMap<>();
        info.put("id", customer.getId());
        info.put("name", customer.getName());
        info.put("contact", customer.getContact());
        info.put("address", customer.getAddress());
        return info;
    }
} 